package b3.widget.home.bottom
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.build.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.vip.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class FriendItem extends Sprite
	{
		private var userId:int;
		private var imgSprite:Sprite;
		private var mouseOverText:TextField;
		private var pictLdr:Loader;
		
		public function FriendItem(userId:int, ranking:int)
		{
			super();
			this.userId = userId;
			var friend:Player = PlayerManager.getInstance().players[userId];
			var container:Sprite = new Sprite();
			addChild(container);
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground();
			container.addChild(tabItemBackground);
			//图片
			imgSprite = new Sprite();
			container.addChild(imgSprite);
			refreshImg();
			//标题名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
        	if (friend == null || friend.name.length < 6)
        	{
        		titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x0088FF, true);
        	}
        	else
        	{
        		titleText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0088FF, true);
        	}
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			if (friend != null)
			{ 
				titleText.text = friend.name.substr(0,4);
			}
			else
			{
				titleText.text = "邀请好友";
			}
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 85;
			container.addChild(titleText);
			if (friend != null && friend.type != PlayerManager.CENTER_TYPE)
			{
				//人口
				var PopulationIcon:Class = ResourceHelper.getInstance().getClassByName("PopulationIcon");
				var populationIcon:Sprite = new PopulationIcon();
				populationIcon.height = 22;
				populationIcon.scaleX = populationIcon.scaleY;
				populationIcon.x = 8;
				populationIcon.y = 8;
				container.addChild(populationIcon);
				//人口值
				var populationText:TextField = new TextField();
				populationText.selectable = false;
	        	populationText.autoSize = TextFieldAutoSize.LEFT;
	    		populationText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x0088FF, true);
				populationText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				populationText.text = BFont.formatNumWithComa(friend.historyMaxPopulation);
				populationText.x = populationIcon.x + 22;
				populationText.y = 8;
				container.addChild(populationText);
				if (friend.is_yellow_vip || friend.is_yellow_year_vip)
				{
					var vipIcon:VipIcon = new VipIcon(friend.yellow_vip_level, friend.is_yellow_year_vip);
					vipIcon.x = 90 - vipIcon.width;
					vipIcon.y = -8;
					addChild(vipIcon);
				}
			}
			//鼠标悬浮信息
			mouseOverText = new TextField();
			mouseOverText.selectable = false;
        	mouseOverText.autoSize = TextFieldAutoSize.LEFT;
    		mouseOverText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x00AA00, true);
			mouseOverText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			if (friend != null)
			{ 
				mouseOverText.text = "点击跳转";
			}
			else
			{
				mouseOverText.text = "点击邀请";
			}
			mouseOverText.x = (tabItemBackground.width - mouseOverText.width) / 2;
			mouseOverText.y = 45;
			mouseOverText.visible = false;
			container.addChild(mouseOverText);
			
			container.addEventListener(MouseEvent.CLICK, onMouseClick);
			container.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			container.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			container.mouseChildren = false;
		}

		private function onMouseClick(event:MouseEvent):void
		{
			var friend:Player = PlayerManager.getInstance().players[userId];
			//好友跳转
			if (friend != null)
			{
				HomeScene.getInstance().loadPlayer(friend);
			}
			//邀请好友
			else
			{
				ServerRequest.inviteFriend();
			}
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			mouseOverText.alpha = 1;
			mouseOverText.visible = true;
			TweenLite.from(mouseOverText, 0.5, {alpha: 0});
		}    
		
		private function onMouseOut(event:MouseEvent):void
		{
			WidgetManager.getInstance().closeMouseOverMessage();
			Mouse.cursor = "auto";
			this.filters = null;
			mouseOverText.visible = false;
		}
		 
		private function refreshImg():void
		{
			var ObjectImage:Class;
			var friend:Player = PlayerManager.getInstance().players[userId];
			//清空头像
			while (imgSprite.numChildren > 0)
				imgSprite.removeChildAt(0);
			//好友头像
			if (friend != null && friend.figureurl != null)
			{
				pictLdr = new Loader();
				var pictURLReq:URLRequest = new URLRequest(friend.figureurl);
				pictLdr.load(pictURLReq);
				pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onUserPicLoaded);
				imgSprite.addChild(pictLdr);
			}
			//中立或者邀请头像
			else
			{
				if (friend == null)
					ObjectImage = ResourceHelper.getInstance().getClassByName("JohnDoe");
				else if (userId == 1)
					ObjectImage = ResourceHelper.getInstance().getClassByName("IconCBD");
				if (ObjectImage != null)
				{
					var img:Sprite = new ObjectImage();
					img.x = (this.width - img.width) / 2;
					img.y = 15;
					imgSprite.addChild(img);
				}
			}
		}
		
		private function onUserPicLoaded(event:Event):void
		{
			pictLdr.x = 48 - pictLdr.width / 2;
			pictLdr.y = 55 - pictLdr.height / 2;
		}
		
	}
}